﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;
using System.Windows;
using System.Data.Sql;
using System.IO;



namespace WowCharacters
{
    class LoadCharacter
    {
        string CharacterName;
        string Realm;
        XDocument _charSheet;
        XDocument _TalantTrees;
        Character LoadedCharacter = new Character();
       
        public LoadCharacter(string Char, string server)
        {
            CharacterName = Char;
            Realm = server;
            System.Net.WebClient _wc = new System.Net.WebClient();
            _wc.QueryString.Add("r", Realm);
            _wc.QueryString.Add("n", CharacterName);
            _wc.Headers.Add("user-agent", "MSIE 7.0");
            System.Xml.XmlTextReader _reader = new System.Xml.XmlTextReader(_wc.OpenRead("http://www.wowarmory.com/character-sheet.xml"));
            _charSheet = XDocument.Load(_reader);
           // System.Xml.XmlTextReader _reader2 = new System.Xml.XmlTextReader(_wc.OpenRead("WowClassTrees.xml"));
            _TalantTrees = XDocument.Load("WowClassTrees.xml");

            
        }
        public  Character GetCharaterStats()
        {
            //Checks for bad character / Error
            var _checkcharacter = from item in _charSheet.Elements("page").Elements("characterInfo")
                                 where item.Attribute("errCode") !=null
                                 select new
                                 {
                                     error = (string)item.Attribute("errCode").Value
                                 };
            foreach (var cc in _checkcharacter)
            {
                if (cc.error == "noCharacter")
                {
                    LoadedCharacter.error = cc.error;

                }
                else
                {

                }
            }
            var _checkcharacter1 = from item in _charSheet.Elements("page").Elements("characterInfo")
                                  where item.Element("characterTab") == null
                                  select new
                                  {
                                      error = "Character Not Updated"
                                  };
            foreach (var cc in _checkcharacter1)
            {
                if (cc.error == "Character Not Updated")
                {
                    LoadedCharacter.error = cc.error;

                }
                else
                {

                }
            }
            //Loads Character if no error
            if (LoadedCharacter.error == null)
            {
                // Gets Base Stats
                DLBaseStats();
                //Gets melee stats
                DLMeleeStats();
                // Gets Ranged Stats
                DLRangedStats();
                // Gets Spell Stats
                DLSpellStats();
                // Gets Defense Stats
                DLDefenseStats();
                // Gets Character INfo
                DLCharInfo();
                //Gets Professions
                DLProfessions();
                //get sBars
                DLCharBars();
                //gets items
                DLItems();
                //gets resist
                DLCharResist();
                //gets talents
                DLTalentSpec();
            }
            else
            {
                
            }
            return LoadedCharacter;
          
        }
        private string GetGuildURL(string guildname)
        {
           
            string GuildURL="";
            if (guildname == "")
            {
                GuildURL = "";
            }
            else
            {
                var _GuildURL = from item in _charSheet.Elements("page").Elements("characterInfo").Elements("character")
                                select new
                                {

                                    URL = (string)item.Attribute("guildUrl").Value
                                };
                foreach (var G in _GuildURL)
                {
                    GuildURL = G.URL;
                }
                
            }
            return GuildURL;
        }
        private void DLBaseStats()
        {
            CharBaseStats CBS = new CharBaseStats();
            var _baseStats = from item in _charSheet.Elements("page").Elements("characterInfo").Elements("characterTab").Elements("baseStats")
                             select new
                             {

                                 StrAttack = (string)item.Element("strength").Attribute("attack").Value,
                                 StrBase = (string)item.Element("strength").Attribute("base").Value,
                                 StrBlock = (string)item.Element("strength").Attribute("block").Value,
                                 StrEffective = (string)item.Element("strength").Attribute("effective").Value,
                                 AgiArmor = (string)item.Element("agility").Attribute("armor").Value,
                                 AgiAttack = (string)item.Element("agility").Attribute("attack").Value,
                                 AgiBase = (string)item.Element("agility").Attribute("base").Value,
                                 AgiCrit = (string)item.Element("agility").Attribute("critHitPercent").Value,
                                 AgiEffective = (string)item.Element("agility").Attribute("effective").Value,
                                 StaBase = (string)item.Element("stamina").Attribute("base").Value,
                                 StaEffective = (string)item.Element("stamina").Attribute("effective").Value,
                                 StaHealth = (string)item.Element("stamina").Attribute("health").Value,
                                 StaPetBonus = (string)item.Element("stamina").Attribute("petBonus").Value,
                                 IntBase = (string)item.Element("intellect").Attribute("base").Value,
                                 IntCrit = (string)item.Element("intellect").Attribute("critHitPercent").Value,
                                 IntEffective = (string)item.Element("intellect").Attribute("effective").Value,
                                 IntMana = (string)item.Element("intellect").Attribute("mana").Value,
                                 IntPetBonus = (string)item.Element("intellect").Attribute("petBonus").Value,
                                 SpiBase = (string)item.Element("spirit").Attribute("base").Value,
                                 SpiEffective = (string)item.Element("spirit").Attribute("effective").Value,
                                 SpiHPRegen = (string)item.Element("spirit").Attribute("healthRegen").Value,
                                 SpiMPRegen = (string)item.Element("spirit").Attribute("manaRegen").Value,
                                 ArmBase = (string)item.Element("armor").Attribute("base").Value,
                                 ArmEffective = (string)item.Element("armor").Attribute("effective").Value,
                                 ArmPercent = (string)item.Element("armor").Attribute("percent").Value,
                                 ArmPetBonus = (string)item.Element("armor").Attribute("petBonus").Value,
                             };
            foreach (var BS in _baseStats)
            {
                CBS.str_attack = BS.StrAttack;
                CBS.str_base = BS.StrBase;
                CBS.str_block = BS.StrBlock;
                CBS.str_effective = BS.StrEffective;
                CBS.agi_armor = BS.AgiArmor;
                CBS.agi_attack = BS.AgiAttack;
                CBS.agi_base = BS.AgiBase;
                CBS.agi_critHitPercent = BS.AgiCrit;
                CBS.agi_effective = BS.AgiEffective;
                CBS.sta_base = BS.StaBase;
                CBS.sta_effective = BS.StaEffective;
                CBS.sta_health = BS.StaHealth;
                CBS.sta_petBonus = BS.StaPetBonus;
                CBS.int_base = BS.IntBase;
                CBS.int_critHitPercent = BS.IntCrit;
                CBS.int_effective = BS.IntEffective;
                CBS.int_mana = BS.IntMana;
                CBS.int_petBonus = BS.IntPetBonus;
                CBS.spi_base = BS.SpiBase;
                CBS.spi_effective = BS.SpiEffective;
                CBS.spi_healthRegen = BS.SpiHPRegen;
                CBS.spi_ManaRegen = BS.SpiMPRegen;
                CBS.arm_base = BS.ArmBase;
                CBS.arm_effective = BS.ArmEffective;
                CBS.arm_percent = BS.ArmPercent;
                CBS.arm_petBonus = BS.ArmPetBonus;

                LoadedCharacter.CBS = CBS;
            }
        }
        private void DLMeleeStats()
        {
            CharMeleeStats CMS = new CharMeleeStats();
            var _meleeStats = from item in _charSheet.Elements("page").Elements("characterInfo").Elements("characterTab").Elements("melee")
                              select new
                              {
                                  MHdps = (string)item.Element("mainHandDamage").Attribute("dps").Value,
                                  MHmax = (string)item.Element("mainHandDamage").Attribute("max").Value,
                                  MHmin = (string)item.Element("mainHandDamage").Attribute("min").Value,
                                  MHpercent = (string)item.Element("mainHandDamage").Attribute("percent").Value,
                                  MHspeed = (string)item.Element("mainHandDamage").Attribute("speed").Value,
                                  MHhastePer = (string)item.Element("mainHandSpeed").Attribute("hastePercent").Value,
                                  MHhasteRate = (string)item.Element("mainHandSpeed").Attribute("hasteRating").Value,
                                  MHhasteValue = (string)item.Element("mainHandSpeed").Attribute("value").Value,
                                  OHdps = (string)item.Element("offHandDamage").Attribute("dps").Value,
                                  OHmax = (string)item.Element("offHandDamage").Attribute("max").Value,
                                  OHmin = (string)item.Element("offHandDamage").Attribute("min").Value,
                                  OHpercent = (string)item.Element("offHandDamage").Attribute("percent").Value,
                                  OHspeed = (string)item.Element("offHandDamage").Attribute("speed").Value,
                                  OHhastePer = (string)item.Element("offHandSpeed").Attribute("hastePercent").Value,
                                  OHhasteRate = (string)item.Element("offHandSpeed").Attribute("hasteRating").Value,
                                  OHhasteValue = (string)item.Element("offHandSpeed").Attribute("value").Value,
                                  APbase = (string)item.Element("power").Attribute("base").Value,
                                  APeffective = (string)item.Element("power").Attribute("effective").Value,
                                  APdpse = (string)item.Element("power").Attribute("increasedDps").Value,
                                  HitPer = (string)item.Element("hitRating").Attribute("increasedHitPercent").Value,
                                  HitRating = (string)item.Element("hitRating").Attribute("value").Value,
                                  CritPer = (string)item.Element("critChance").Attribute("percent").Value,
                                  CritPerPlus = (string)item.Element("critChance").Attribute("plusPercent").Value,
                                  CritRating = (string)item.Element("critChance").Attribute("rating").Value,
                                  ExpAdd = (string)item.Element("expertise").Attribute("additional").Value,
                                  ExpPercent = (string)item.Element("expertise").Attribute("percent").Value,
                                  ExpRating = (string)item.Element("expertise").Attribute("rating").Value,
                                  ExpVal = (string)item.Element("expertise").Attribute("value").Value

                              };
            foreach (var MS in _meleeStats)
            {
                CMS.AP_Base = MS.APbase;
                CMS.AP_DPS = MS.APdpse;
                CMS.AP_effective = MS.APeffective;
                CMS.Crit_Percent = MS.CritPer;
                CMS.Crit_plusPercent = MS.CritPerPlus;
                CMS.Crit_Rating = MS.CritRating;
                CMS.Exp_Add = MS.ExpAdd;
                CMS.Exp_percent = MS.ExpPercent;
                CMS.Exp_Rating = MS.ExpRating;
                CMS.Exp_value = MS.ExpVal;
                CMS.Hit_Percent = MS.HitPer;
                CMS.Hit_Value = MS.HitRating;
                CMS.MH_dps = MS.MHdps;
                CMS.MH_hasteper = MS.MHhastePer;
                CMS.MH_HasteRate = MS.MHhasteRate;
                CMS.MH_HasteSpeed = MS.MHhasteValue;
                CMS.MH_max = MS.MHmax;
                CMS.MH_min = MS.MHmin;
                CMS.MH_percent = MS.MHpercent;
                CMS.MH_speed = MS.MHspeed;
                CMS.OH_dps = MS.OHdps;
                CMS.OH_hasteper = MS.OHhastePer;
                CMS.OH_HasteRate = MS.OHhasteRate;
                CMS.OH_HasteSpeed = MS.OHhasteValue;
                CMS.OH_max = MS.OHmax;
                CMS.OH_min = MS.OHmin;
                CMS.OH_percent = MS.OHpercent;
                CMS.OH_speed = MS.OHspeed;

                LoadedCharacter.CMS = CMS;
            }
        }
        private void DLRangedStats()
        {
            CharRangedStats CRS = new CharRangedStats();
            var _rangedStats = from item in _charSheet.Elements("page").Elements("characterInfo").Elements("characterTab").Elements("ranged")
                               select new
                               {
                                   WSRating = (string)item.Element("weaponSkill").Attribute("rating").Value,
                                   WSValue = (string)item.Element("weaponSkill").Attribute("value").Value,
                                   DDPS = (string)item.Element("damage").Attribute("dps").Value,
                                   DMax = (string)item.Element("damage").Attribute("max").Value,
                                   DMin = (string)item.Element("damage").Attribute("min").Value,
                                   DPercent = (string)item.Element("damage").Attribute("percent").Value,
                                   DSpeed = (string)item.Element("damage").Attribute("speed").Value,
                                   SHPercent = (string)item.Element("speed").Attribute("hastePercent").Value,
                                   SHrating = (string)item.Element("speed").Attribute("hasteRating").Value,
                                   SValue = (string)item.Element("speed").Attribute("value").Value,
                                   PBase = (string)item.Element("power").Attribute("base").Value,
                                   PEffective = (string)item.Element("power").Attribute("effective").Value,
                                   PDps = (string)item.Element("power").Attribute("increasedDps").Value,
                                   PPetAttack = (string)item.Element("power").Attribute("petAttack").Value,
                                   PPetSpell = (string)item.Element("power").Attribute("petSpell").Value,
                                   HitPer = (string)item.Element("hitRating").Attribute("increasedHitPercent").Value,
                                   HitRating = (string)item.Element("hitRating").Attribute("value").Value,
                                   CritPer = (string)item.Element("critChance").Attribute("percent").Value,
                                   CritPerPlus = (string)item.Element("critChance").Attribute("plusPercent").Value,
                                   CritRating = (string)item.Element("critChance").Attribute("rating").Value,

                               };
            foreach (var RS in _rangedStats)
            {
                CRS.Crit_Percent = RS.CritPer;
                CRS.Crit_plusPercent = RS.CritPerPlus;
                CRS.Crit_Rating = RS.CritRating;
                CRS.D_DPS = RS.DDPS;
                CRS.D_Max = RS.DMax;
                CRS.D_Min = RS.DMin;
                CRS.D_Percent = RS.DPercent;
                CRS.D_Speed = RS.DSpeed;
                CRS.Hit_Percent = RS.HitPer;
                CRS.Hit_Value = RS.HitRating;
                CRS.P_Base = RS.PBase;
                CRS.P_Effective = RS.PEffective;
                CRS.P_increasedDps = RS.PDps;
                CRS.P_PetAttack = RS.PPetAttack;
                CRS.P_PetSpell = RS.PPetSpell;
                CRS.S_HastePEr = RS.SHPercent;
                CRS.S_HasteRate = RS.SHrating;
                CRS.S_Value = RS.SValue;
                CRS.WS_Rating = RS.WSRating;
                CRS.WS_Value = RS.WSValue;
                LoadedCharacter.CRS = CRS;
            }
        }
        private void DLSpellStats()
        {
            CharSpellStats CSS = new CharSpellStats();
            var _spellStats = from item in _charSheet.Elements("page").Elements("characterInfo").Elements("characterTab").Elements("spell")
                              select new
                              {
                                  BD_Arcane = (string)item.Element("bonusDamage").Element("arcane").Attribute("value").Value,
                                  BD_Fire = (string)item.Element("bonusDamage").Element("fire").Attribute("value").Value,
                                  BD_Frost = (string)item.Element("bonusDamage").Element("frost").Attribute("value").Value,
                                  BD_Holy = (string)item.Element("bonusDamage").Element("holy").Attribute("value").Value,
                                  BD_Nature = (string)item.Element("bonusDamage").Element("nature").Attribute("value").Value,
                                  BD_Shadow = (string)item.Element("bonusDamage").Element("shadow").Attribute("value").Value,
                                  BD_PetAP = (string)item.Element("bonusDamage").Element("petBonus").Attribute("attack").Value,
                                  BD_PetDamage = (string)item.Element("bonusDamage").Element("petBonus").Attribute("damage").Value,
                                  BD_PetFromType = (string)item.Element("bonusDamage").Element("petBonus").Attribute("fromType").Value,
                                  BHealing = (string)item.Element("bonusHealing").Attribute("value").Value,
                                  HitPer = (string)item.Element("hitRating").Attribute("increasedHitPercent").Value,
                                  HitRating = (string)item.Element("hitRating").Attribute("value").Value,
                                  Crit_Rate = (string)item.Element("critChance").Attribute("rating").Value,
                                  Crit_Arcane = (string)item.Element("critChance").Element("arcane").Attribute("percent").Value,
                                  Crit_Fire = (string)item.Element("critChance").Element("fire").Attribute("percent").Value,
                                  Crit_Frost = (string)item.Element("critChance").Element("frost").Attribute("percent").Value,
                                  Crit_Holy = (string)item.Element("critChance").Element("holy").Attribute("percent").Value,
                                  Crit_Nature = (string)item.Element("critChance").Element("nature").Attribute("percent").Value,
                                  Crit_Shadow = (string)item.Element("critChance").Element("shadow").Attribute("percent").Value,
                                  Penetration = (string)item.Element("penetration").Attribute("value").Value,
                                  MP5_cast = (string)item.Element("manaRegen").Attribute("casting").Value,
                                  MP5_noncast = (string)item.Element("manaRegen").Attribute("notCasting").Value,


                              };
            foreach (var SS in _spellStats)
            {
                CSS.BDamage_Arcane = SS.BD_Arcane;
                CSS.BDamage_Fire = SS.BD_Fire;
                CSS.BDamage_Frost = SS.BD_Frost;
                CSS.BDamage_Holy = SS.BD_Holy;
                CSS.BDamage_Nature = SS.BD_Nature;
                CSS.BDamage_PetAP = SS.BD_PetAP;
                CSS.BDamage_PetDamage = SS.BD_PetDamage;
                CSS.BDamage_PetFromType = SS.BD_PetFromType;
                CSS.BDamage_Shadow = SS.BD_Shadow;
                CSS.BHealing = SS.BHealing;
                CSS.Crit_Arcane = SS.Crit_Arcane;
                CSS.Crit_Fire = SS.Crit_Fire;
                CSS.Crit_Frost = SS.Crit_Frost;
                CSS.Crit_Holy = SS.Crit_Holy;
                CSS.Crit_Nature = SS.Crit_Nature;
                CSS.Crit_Rating = SS.Crit_Rate;
                CSS.Crit_Shadow = SS.Crit_Shadow;
                CSS.Hit_Percent = SS.HitPer;
                CSS.Hit_Value = SS.HitRating;
                CSS.MP5_Cast = SS.MP5_cast;
                CSS.MP5_Noncast = SS.MP5_noncast;
                CSS.Penetration = SS.Penetration;
                LoadedCharacter.CSS = CSS;

            }
        }
        private void DLDefenseStats()
        {
            CharDefenseStats CDS = new CharDefenseStats();
            var _defenseStats = from item in _charSheet.Elements("page").Elements("characterInfo").Elements("characterTab").Elements("defenses")
                                select new
                                {
                                    ArmBase = (string)item.Element("armor").Attribute("base").Value,
                                    ArmEff = (string)item.Element("armor").Attribute("effective").Value,
                                    ArmPer = (string)item.Element("armor").Attribute("percent").Value,
                                    ArmPet = (string)item.Element("armor").Attribute("petBonus").Value,
                                    DefDecreasePer = (string)item.Element("defense").Attribute("decreasePercent").Value,
                                    DefIncreasePer = (string)item.Element("defense").Attribute("increasePercent").Value,
                                    DefPlusDef = (string)item.Element("defense").Attribute("plusDefense").Value,
                                    DefRating = (string)item.Element("defense").Attribute("rating").Value,
                                    DefValue = (string)item.Element("defense").Attribute("value").Value,
                                    DodgePlusPer = (string)item.Element("dodge").Attribute("increasePercent").Value,
                                    DodgePer = (string)item.Element("dodge").Attribute("percent").Value,
                                    DodgeRating = (string)item.Element("dodge").Attribute("rating").Value,
                                    ParryPlusPer = (string)item.Element("parry").Attribute("increasePercent").Value,
                                    ParryPer = (string)item.Element("parry").Attribute("percent").Value,
                                    ParryRating = (string)item.Element("parry").Attribute("rating").Value,
                                    BlockPlusPer = (string)item.Element("block").Attribute("increasePercent").Value,
                                    BlockPer = (string)item.Element("block").Attribute("percent").Value,
                                    BlockRating = (string)item.Element("block").Attribute("rating").Value,
                                    ResDamPercent = (string)item.Element("resilience").Attribute("damagePercent").Value,
                                    ResPercent = (string)item.Element("resilience").Attribute("hitPercent").Value,
                                    ResValue = (string)item.Element("resilience").Attribute("value").Value,


                                };
            foreach (var DS in _defenseStats)
            {
                CDS.Arm_Base = DS.ArmBase;
                CDS.Arm_Effective = DS.ArmEff;
                CDS.Arm_Percent = DS.ArmPer;
                CDS.Arm_PetBonus = DS.ArmPet;
                CDS.Block_Percent = DS.BlockPer;
                CDS.Block_PlusPercent = DS.BlockPlusPer;
                CDS.Block_Rating = DS.BlockRating;
                CDS.Def_DecreasePercent = DS.DefDecreasePer;
                CDS.Def_IncreasePercent = DS.DefIncreasePer;
                CDS.Def_PlusDefense = DS.DefPlusDef;
                CDS.Def_Rating = DS.DefRating;
                CDS.Def_Value = DS.DefValue;
                CDS.Dodge_Percent = DS.DodgePer;
                CDS.Dodge_PlusPercent = DS.DodgePlusPer;
                CDS.Dodge_Rating = DS.DodgeRating;
                CDS.Parry_Percent = DS.ParryPer;
                CDS.Parry_PlusPercent = DS.ParryPlusPer;
                CDS.Parry_Rating = DS.ParryRating;
                CDS.Res_DamagePercent = DS.ResDamPercent;
                CDS.Res_HitPercent = DS.ResPercent;
                CDS.Res_Value = DS.ResValue;
                LoadedCharacter.CDS = CDS;

            }
        }
        private void DLTalentSpec()
        {
            
            CharTalent CT = new CharTalent();
            var _talents = from item in _charSheet.Elements("page").Elements("characterInfo")
                                select new
                                {
                                    Treeone = (string)item.Element("characterTab").Element("talentSpec").Attribute("treeOne").Value,
                                    TreeTwo = (string)item.Element("characterTab").Element("talentSpec").Attribute("treeTwo").Value,
                                    TreeThree = (string)item.Element("characterTab").Element("talentSpec").Attribute("treeThree").Value,
                                    CClass = (string)item.Element("character").Attribute("class").Value,
                                    CClassID = (string)item.Element("character").Attribute("classId").Value


                                };
            foreach (var t in _talents)
            {
                
                
                var _treename = from item in _TalantTrees.Elements("talents")
                                select new

                                {
                                    TreeONe = (string)item.Element(t.CClass.ToLower().Replace(" ","")).Attribute("treeONe").Value,
                                    TreeTwo = (string)item.Element(t.CClass.ToLower().Replace(" ", "")).Attribute("treeTwo").Value,
                                    TreeThree = (string)item.Element(t.CClass.ToLower().Replace(" ", "")).Attribute("treeThree").Value,

                                };
                foreach (var SN in _treename)
                {
                    
                    CT.TreeOne = SN.TreeONe;
                    CT.TreeOne_Value = t.Treeone;
                    CT.TreeOne_Image = t.CClassID + "-1.gif";
                    CT.TreeTwo = SN.TreeTwo;
                    CT.TreeTwo_Value = t.TreeTwo;
                    CT.TreeTwo_Image = t.CClassID + "-2.gif";
                    CT.TreeThree = SN.TreeThree;
                    CT.TreeThree_Value = t.TreeThree;
                    CT.TreeThree_Image = t.CClassID + "-3.gif";

                    for (int i = 1; i < 4; i++)
                    {
                        string imagename = t.CClassID + "-" + Convert.ToString(i) + ".gif";
                        string ImageURl = "http://www.wowarmory.com/images/icons/class/" + t.CClassID + "/talents/" + Convert.ToString(i) + ".gif";
                        if (File.Exists(imagename))
                        {

                        }
                        else
                        {
                            System.Net.WebClient Client = new System.Net.WebClient();
                            Client.DownloadFile(ImageURl, imagename);
                        }

                    }
                    
                }
                

            }
            LoadedCharacter.CT = CT;
        }
        private void DLCharInfo()
        {
            CharInfo CI = new CharInfo();
            var _charInfo = from item in _charSheet.Elements("page").Elements("characterInfo").Elements("character")
                            select new
                            {
                                Battlegroup = (string)item.Attribute("battleGroup").Value,
                                CharURL = (string)item.Attribute("charUrl").Value,
                                Class = (string)item.Attribute("class").Value,
                                ClassID = (string)item.Attribute("classId").Value,
                                Faction = (string)item.Attribute("faction").Value,
                                FactionID = (string)item.Attribute("factionId").Value,
                                Gender = (string)item.Attribute("gender").Value,
                                GenderID = (string)item.Attribute("genderId").Value,
                                GuildName = (string)item.Attribute("guildName").Value,
                                GuildURL = GetGuildURL(item.Attribute("guildName").Value),
                                LastModified = (string)item.Attribute("lastModified").Value,
                                Level = (string)item.Attribute("level").Value,
                                Name = (string)item.Attribute("name").Value,
                                Prefix = (string)item.Attribute("prefix").Value,
                                Race = (string)item.Attribute("race").Value,
                                RaceID = (string)item.Attribute("raceId").Value,
                                Realm = (string)item.Attribute("realm").Value,
                                Suffix = (string)item.Attribute("suffix").Value

                            };

            foreach (var C in _charInfo)
            {
                string portraitURL = "http://www.wowarmory.com/images/portraits/";
                string portraitFile = C.GenderID + "-" + C.RaceID + "-" + C.ClassID + ".gif";
                string portraitFolder;
                CI.BattleGroup = C.Battlegroup;
                CI.CharUrl = C.CharURL;
                CI.Class = C.Class;
                CI.ClassID = C.ClassID;
                CI.Faction = C.Faction;
                CI.FactionID = C.FactionID;
                CI.Gender = C.Gender;
                CI.GenderID = C.GenderID;
                CI.GuildName = C.GuildName;
                CI.guildURL = C.GuildURL;
                CI.LastModified = C.LastModified;
                CI.Level = C.Level;
                CI.Name = C.Name;
                CI.Prefix = C.Prefix;
                CI.Race = C.Race;
                CI.RaceID = C.RaceID;
                CI.Realm = C.Realm;
                CI.Suffix = C.Suffix;

                
  
                //finds the character portraite
                if (Convert.ToInt32(CI.Level) < 60)
                {
                    portraitFolder = "wow-default/";
                    if (File.Exists(CI.Level + "-"+ portraitFile))
                    {
                    }
                    else
                    {
                        System.Net.WebClient Client = new System.Net.WebClient();
                        Client.DownloadFile(portraitURL + portraitFolder + portraitFile, CI.Level + "-" + portraitFile);
                    }

                }
                else if (Convert.ToInt32(CI.Level) > 59 && Convert.ToInt32(CI.Level) < 70)
                {
                    portraitFolder = "wow/";
                    if (File.Exists(CI.Level + "-"+ portraitFile))
                    {
                    }
                    else
                    {
                        System.Net.WebClient Client = new System.Net.WebClient();
                        Client.DownloadFile(portraitURL + portraitFolder + portraitFile, CI.Level + "-"+ portraitFile);
                    }

                }
                else if (Convert.ToInt32(CI.Level) > 69 && Convert.ToInt32(CI.Level) < 80)
                {
                    portraitFolder = "wow-70/";
                    if (File.Exists(CI.Level + "-"+ portraitFile))
                    {
                    }
                    else
                    {
                        System.Net.WebClient Client = new System.Net.WebClient();
                        Client.DownloadFile(portraitURL + portraitFolder + portraitFile, CI.Level + "-"+ portraitFile);
                    }

                }
                else
                {
                    portraitFolder = "wow-80/";
                    if (File.Exists(CI.Level + "-"+ portraitFile))
                    {
                    }
                    else
                    {
                        System.Net.WebClient Client = new System.Net.WebClient();
                        Client.DownloadFile(portraitURL + portraitFolder + portraitFile, CI.Level + "-"+ portraitFile);
                    }

                }
                portraitFile = CI.Level + "-" + portraitFile;
                CI.PortraitFileName = portraitFile;
                LoadedCharacter.CI = CI;
            }
        }
        private void DLProfessions()
        {
            CharTradeSkills CTS = new CharTradeSkills();
            var _profs = from item in _charSheet.Elements("page").Elements("characterInfo").Elements("characterTab").Elements("professions").Elements("skill")
                         select new
                         {
                             Key = item.Attribute("key").Value,
                             Name = item.Attribute("name").Value,
                             Max = item.Attribute("max").Value,
                             Value = item.Attribute("value").Value
                         };
            int count = 0;

            foreach (var p in _profs)
            {

                string tradeURL = "http://www.wowarmory.com/images/icons/professions/";
                string TradeskillFile = p.Key + "-sm.gif";
                if (File.Exists(TradeskillFile))
                {
                }
                else
                {
                    System.Net.WebClient Client = new System.Net.WebClient();
                    Client.DownloadFile(tradeURL + TradeskillFile, TradeskillFile);
                }
                if (count == 0)
                {
                    CTS.P1_Icon = TradeskillFile;
                    CTS.P1_max = p.Max;
                    CTS.P1_Name = p.Name;
                    CTS.P1_Value = p.Value;
                    count = count + 1;
                }
                else if (count == 1)
                {
                    CTS.P2_Icon = TradeskillFile;
                    CTS.P2_Max = p.Max;
                    CTS.P2_Name = p.Name;
                    CTS.P2_Value = p.Value;
                    count = count + 1;
                }
                else
                {
                }
            }
            string tradeURL2 = "http://www.wowarmory.com/images/icons/professions/";
            string TradeskillFile2 = "none-sm.gif";
            if (File.Exists(TradeskillFile2))
            {
            }
            else
            {
                System.Net.WebClient Client = new System.Net.WebClient();
                Client.DownloadFile(tradeURL2 + TradeskillFile2, TradeskillFile2);
            }
            if (count == 1)
            {
                CTS.P2_Icon = TradeskillFile2;
                CTS.P2_Max = "0";
                CTS.P2_Name = "None";
                CTS.P2_Value = "0";
            }
            else if (count == 0)
            {
                CTS.P1_Icon = TradeskillFile2;
                CTS.P1_max = "0";
                CTS.P1_Name = "None";
                CTS.P1_Value = "0";
                CTS.P2_Icon = TradeskillFile2;
                CTS.P2_Max = "0";
                CTS.P2_Name = "None";
                CTS.P2_Value = "0";
            }
            LoadedCharacter.CTS = CTS;
        }
        private void DLCharBars()
        {
            CharBars CB = new CharBars();
            var _bar = from item in _charSheet.Elements("page").Elements("characterInfo").Elements("characterTab").Elements("characterBars")
                       select new
                       {
                           Health = (string)item.Element("health").Attribute("effective").Value,
                           SecBar_effective = (string)item.Element("secondBar").Attribute("effective").Value,
                           SecBar_type = (string)item.Element("secondBar").Attribute("type").Value
                       };

            foreach (var b in _bar)
            {
                CB.Health = b.Health;
                CB.SecBar_effective = b.SecBar_effective;
                if (b.SecBar_type == "e")
                {
                    CB.SecBar_type = "Energy";
                    CB.SecBar_Color = System.Drawing.Color.FromArgb(165, 165, 0);

                }
                else if (b.SecBar_type == "r")
                {
                    CB.SecBar_type = "RAGE";
                    CB.SecBar_Color = System.Drawing.Color.FromArgb(175, 0, 0);
                }
                else if (b.SecBar_type == "m")
                {
                    CB.SecBar_type = "MANA";
                    CB.SecBar_Color = System.Drawing.Color.FromArgb(0, 0, 150);
                }
                else if (b.SecBar_type == "p")
                {
                    CB.SecBar_type = "RUNIC";
                    CB.SecBar_Color = System.Drawing.Color.FromArgb(0, 192, 192);
                }
                else
                {
                }


            }
            LoadedCharacter.CB = CB;
        }
        private void DLItems()
        {
            CharItems[] items = new CharItems[20];
            CharItems slot0 = new CharItems();
            CharItems slot1 = new CharItems();
            CharItems slot2 = new CharItems();
            CharItems slot3 = new CharItems();
            CharItems slot4 = new CharItems();
            CharItems slot5 = new CharItems();
            CharItems slot6 = new CharItems();
            CharItems slot7 = new CharItems();
            CharItems slot8 = new CharItems();
            CharItems slot9 = new CharItems();
            CharItems slot10 = new CharItems();
            CharItems slot11 = new CharItems();
            CharItems slot12 = new CharItems();
            CharItems slot13 = new CharItems();
            CharItems slot14 = new CharItems();
            CharItems slot15 = new CharItems();
            CharItems slot16 = new CharItems();
            CharItems slot17 = new CharItems();
            CharItems slot18 = new CharItems();
            CharItems slot19 = new CharItems();
            items[0] = slot0;
            items[1] = slot1;
            items[2] = slot2;
            items[3] = slot3;
            items[4] = slot4;
            items[5] = slot5;
            items[6] = slot6;
            items[7] = slot7;
            items[8] = slot8;
            items[9] = slot9;
            items[10] = slot10;
            items[11] = slot11;
            items[12] = slot12;
            items[13] = slot13;
            items[14] = slot14;
            items[15] = slot15;
            items[16] = slot16;
            items[17] = slot17;
            items[18] = slot18;
            items[19] = slot19;

             
            var _items = from item in _charSheet.Elements("page").Elements("characterInfo").Elements("characterTab").Elements("items").Elements("item")
                         
                         select new
                         {
                             durability = (string)item.Attribute("durability").Value,
                             gem0id = (string)item.Attribute("gem0Id").Value,
                             gem1id = (string)item.Attribute("gem1Id").Value,
                             gem2id = (string)item.Attribute("gem2Id").Value,
                             icon = "http://www.wowarmory.com/wow-icons/_images/64x64/" + (string)item.Attribute("icon").Value + ".jpg",
                             file = (string)item.Attribute("icon").Value + ".jpg",
                             id = (string)item.Attribute("id").Value,
                             maxDuarability = (string)item.Attribute("maxDurability").Value,
                             permanentenchant = (string)item.Attribute("permanentenchant").Value,
                             randomPropertiesID = (string)item.Attribute("randomPropertiesId").Value,
                             seed = (string)item.Attribute("seed").Value,
                             slot = (string)item.Attribute("slot").Value
                         };
            foreach (var i in _items)
            {
                // gets basic information
                int slotID = Convert.ToInt32(i.slot);
                if (slotID == -1)
                {

                    slotID = 19;
                }
                else
                {
                }
                items[slotID].druability = i.durability;
                items[slotID].gem0ID = i.gem0id;
                items[slotID].gem1ID = i.gem1id;
                items[slotID].gem2Id = i.gem2id;
                items[slotID].icon = i.icon;
                items[slotID].file = i.file;
                items[slotID].id = i.id;
                items[slotID].maxDurability = i.maxDuarability;
                items[slotID].permanentechant = i.permanentenchant;
                items[slotID].randomProperties = i.randomPropertiesID;
                items[slotID].seed = i.seed;
                items[slotID].slot = Convert.ToString(slotID);
                //gets images
                if (File.Exists(i.file))
                {

                }
                else
                {
                    System.Net.WebClient Client = new System.Net.WebClient();
                    Client.DownloadFile(i.icon, i.file);
                }
                //Gets Item Name
                XDocument _ItemPage;
                System.Net.WebClient _wc1 = new System.Net.WebClient();
                _wc1.QueryString.Add("i", i.id);
                _wc1.Headers.Add("user-agent", "MSIE 7.0");
                System.Xml.XmlTextReader _reader1 = new System.Xml.XmlTextReader(_wc1.OpenRead("http://www.wowarmory.com/item-tooltip.xml"));
                _ItemPage = XDocument.Load(_reader1);
                IEnumerable<XElement> _ItemInfoEl = _ItemPage.Root.Descendants("itemTooltip");

                var _ItemInfo = from item in _ItemInfoEl
                                select new
                                {
                                    name = item.Element("name").Value,
                                };
                foreach (var Iteminfo in _ItemInfo)
                {
                    items[slotID].itemname = Iteminfo.name;
                }
            }
            LoadedCharacter.CIT = items;
             
        }
        private void DLCharResist()
        {
            string ImageURL = "http://www.wowarmory.com/images/";
            CharResists CR = new CharResists();
            // download images
     
            var _resistsimage = from item in _charSheet.Elements("page").Elements("characterInfo").Elements("characterTab").Elements("resistances").Descendants()
                                select new
                                {
                                    resistname = item.Name.ToString()
                                };
            foreach (var resname in _resistsimage)
            {
                string imagename = "res-" + resname.resistname + ".gif";
                if (File.Exists(imagename))
                {

                }
                else
                {
                    
                    System.Net.WebClient Client = new System.Net.WebClient();
                    Client.DownloadFile(ImageURL + imagename, imagename);
                }
            }
            var _resists = from item in _charSheet.Elements("page").Elements("characterInfo").Elements("characterTab").Elements("resistances")
                         select new
                         
                         {
                             Arcane = (string)item.Element("arcane").Attribute("value").Value,
                             ArccanePet = (string)item.Element("arcane").Attribute("petBonus").Value,
                             AcraneImage = "res-arcane.gif",
                             Fire = (string)item.Element("fire").Attribute("value").Value,
                             FirePet = (string)item.Element("fire").Attribute("petBonus").Value,
                             FireImage = "res-fire.gif",
                             Frost = (string)item.Element("frost").Attribute("value").Value,
                             FrostPet = (string)item.Element("frost").Attribute("petBonus").Value,
                             FrostImage = "res-frost.gif",
                             Holy = (string)item.Element("holy").Attribute("value").Value,
                             HolyPet = (string)item.Element("holy").Attribute("petBonus").Value,
                             HOlyImage = "res-holy.gif",
                             Nature = (string)item.Element("nature").Attribute("value").Value,
                             NaturePet = (string)item.Element("nature").Attribute("petBonus").Value,
                             NatureImage = "res-nature.gif",
                             Shadow = (string)item.Element("shadow").Attribute("value").Value,
                             ShadowPet = (string)item.Element("shadow").Attribute("petBonus").Value,
                             ShadowImage = "res-shadow.gif",
                         };
            foreach (var res in _resists)
            {
                CR.Arcane = res.Arcane;
                CR.Arcane_Image = res.AcraneImage;
                CR.Arcane_Pet = res.ArccanePet;
                CR.Fire = res.Fire;
                CR.Fire_Image = res.FireImage;
                CR.Fire_Pet = res.FirePet;
                CR.Frost = res.Frost;
                CR.Frost_Image = res.FrostImage;
                CR.Frost_Pet = res.FrostPet;
                CR.Holy = res.Holy;
                CR.Holy_Image = res.HOlyImage;
                CR.Holy_Pet = res.HolyPet;
                CR.Nature = res.Nature;
                CR.Nature_Image = res.NatureImage;
                CR.Nature_Pet = res.NaturePet;
                CR.Shadow = res.Shadow;
                CR.Shadow_Image = res.ShadowImage;
                CR.Shadow_Pet = res.ShadowPet;

                LoadedCharacter.CR = CR;

               
            }

        }

    }
}
